package query

import (
	"errors"
	"octopus/hextech/nlog"
	"octopus/user/infra/po"

	"gorm.io/gorm"
)

func (s *Store) UserExist(email, mobile string) (exist bool, err error) {
	user := po.User{}
	res := s.db.WithContext(s.ctx).Where("email = ? OR mobile = ?", email, mobile).First(&user)
	if res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) {
		return false, err
	} else if errors.Is(res.Error, gorm.ErrRecordNotFound) {
		return false, nil
	}
	return true, nil
}

func (s *Store) UserInfoByID(userID int64) (resp *po.User, has bool, err error) {
	res := s.db.WithContext(s.ctx).Where("id = ?", userID).First(&resp)
	if res.Error != nil && !errors.Is(res.Error, gorm.ErrRecordNotFound) {
		nlog.Warn(s.ctx, "query customer info by customer id error:", res.Error, "params is : ", userID)
		return nil, false, err
	} else if errors.Is(res.Error, gorm.ErrRecordNotFound) {
		return nil, true, nil
	}
	return resp, true, nil
}
